package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.mapper.EmployeeMapper;
import cn.wolfcode.rbac.query.EmployeeQueryObject;
import cn.wolfcode.rbac.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper mapper;

    public void saveOrUpdate(Employee u, int[] ids) {
        if (u.getId() == null) {
            Md5Hash password = new Md5Hash(u.getPassword(),u.getName());
            u.setPassword(password.toString());
            mapper.insert(u);
        } else {
            mapper.deleteRoles(u.getId());
            mapper.updateByPrimaryKey(u);
        }
        if (ids != null && ids.length >= 0) {
            for (int id : ids) {
                mapper.addrole(u.getId(), id);
            }
        }
    }


    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
        mapper.deleteRoles(id);
    }


    public PageInfo query(EmployeeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> result = mapper.queryForList(qo);
        return new PageInfo(result);
    }


    public Employee selectOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public Employee selectByInfo(String username, String password) {
        if(StringUtils.hasLength(username)){
            if (StringUtils.hasLength(password)){
                Employee employee = mapper.selectByInfo(username, password);
                if(employee!=null){
                    return employee;
                }else{
                    throw new RuntimeException("账号与密码不匹配");
                }
            }else{
                throw new RuntimeException("密码为空");
            }
        }else{
            throw new RuntimeException("账号为空");
        }

    }

    public Employee selectByName(String name) {
        return mapper.selectByName(name);
    }

    public void deleteMany(Long[] ids) {
        mapper.deleteMany(ids);
    }

    public Workbook exportXls() {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet = wb.createSheet("employee");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        List<Employee> list=mapper.selectAllForXls();
        int i=1;
        for(Employee e:list){
            Row row1 = sheet.createRow(i);
            row1.createCell(0).setCellValue(e.getName());
            row1.createCell(1).setCellValue(e.getEmail());
            row1.createCell(2).setCellValue(e.getAge());
            i++;
        }
        return wb;
    }

    public void importXls(MultipartFile file) throws IOException {
        Workbook wb=new HSSFWorkbook(file.getInputStream());
        Sheet sheet = wb.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for(int i=1;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String email = row.getCell(1).getStringCellValue();
            Cell cell = row.getCell(2);
            Integer age=null;
            switch (cell.getCellTypeEnum()){
                case STRING:
                    age=Integer.valueOf(cell.getStringCellValue());
                    break;
                case NUMERIC:
                    age=(int)cell.getNumericCellValue();
                    break;
            }
            Employee e=new Employee();
            e.setName(name);
            e.setEmail(email);
            e.setAge(age);
            e.setPassword("1");
            this.saveOrUpdate(e,null);
        }
    }

    public List<Employee> selectBySn(String ...sn) {
        return mapper.selectBySn(sn);
    }
}
